package com.dbappsecurity.network.client.interceptor;

import com.google.gson.Gson;
import okhttp3.*;
import okio.Buffer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.dbappsecurity.network.api.ApiHelper.JsonMediaType;

/**
 * Created by Arirus on 2017/8/18.
 */

public final class TraversalParameterInterceptor implements Interceptor {

    private static final String TAG = " TraversalParameter";

    public Map<String, String> transfer(Map<String, String> map) {
        return null;
    }

    private InterceptorHelper.Transformer transformer = null;

    public void setTransfer(InterceptorHelper.Transformer transfer) {
        transformer = transfer;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl url = request.url();
        Gson gson = new Gson();
        //url.newBuilder().
        Map<String, String> rootMap = new HashMap<>();
        //通过请求地址(最初始的请求地址)获取到参数列表
        if (request.method().equals("GET")) {
            Set<String> parameterNames = url.queryParameterNames();
            for (String key : parameterNames) {  //循环参数列表
                rootMap.put(key, url.queryParameter(key));
            }
        } else if (request.method().equals("POST") || request.method().equals("PUT")) {
            RequestBody requestBody = request.body();
            if (requestBody instanceof FormBody) {
                for (int i = 0; i < ((FormBody) requestBody).size(); i++) {
                    rootMap.put(((FormBody) requestBody).encodedName(i),
                            ((FormBody) requestBody).encodedValue(i));
                }
            } else {
                Buffer buffer = new Buffer();
                requestBody.writeTo(buffer);
                String oldParamsJson = buffer.readUtf8();
            }
        }

        for (String key : rootMap.keySet()) {
        }

        if (transformer != null)
            rootMap = transformer.trans(rootMap);

        for (String key : rootMap.keySet()) {
        }

        Request.Builder requestBuilder = request.newBuilder();
        Request newRequest;

        if (request.method().equals("GET")) {
            HttpUrl.Builder urlBuilder = url.newBuilder();
            for (String key : rootMap.keySet()) {
                urlBuilder.removeAllEncodedQueryParameters(key);
            }
            url = urlBuilder.build();
            newRequest = requestBuilder.url(url).build();
            return chain.proceed(newRequest);
        } else if (request.method().equals("POST") || request.method().equals("PUT")) {
            if (request.body() instanceof FormBody) {
                FormBody.Builder builder = new FormBody.Builder();
                for (String key : rootMap.keySet()) {
                    builder.addEncoded(key, rootMap.get(key));
                }
                newRequest = requestBuilder.post(builder.build()).build();
                return chain.proceed(newRequest);
            } else {
                MediaType JSON = JsonMediaType();
                if (!request.body().contentType().equals(JSON)) return chain.proceed(request);
                Buffer buffer = new Buffer();
                request.body().writeTo(buffer);
                String oldParamsJson = buffer.readUtf8();
//        String content = AES.encrypt((oldParamsJson));
//        if (request.method().equals("POST"))
////          newRequest = requestBuilder.post(RequestBody.create(JSON, content)).build();
//        else
//          newRequest = requestBuilder.put(RequestBody.create(JSON, content)).build();
                return chain.proceed(request);
            }
        }

        return null;
    }
}
